package com.bl.weilan.manager.controller.tradepolicy;

import com.bl.weilan.manager.controller.BaseController;
import com.bl.weilan.common.ApiResponseBody;
import com.bl.weilan.service.biz.institution.InstitutionService;
import com.bl.weilan.service.biz.tradepolicy.TradePolicyService;
import com.bl.weilan.service.vo.DelBody;
import com.bl.weilan.service.vo.DelListBody;
import com.bl.weilan.service.vo.institution.InstitutionAddVo;
import com.bl.weilan.service.vo.institution.InstitutionModVo;
import com.bl.weilan.service.vo.news.NewsAddVo;
import com.bl.weilan.service.vo.tradepolicy.TradePolicyAddVo;
import com.bl.weilan.service.vo.tradepolicy.TradePolicyListVo;
import com.bl.weilan.service.vo.tradepolicy.TradePolicyModVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


@RestController
@Log4j2
@RequestMapping(value = {"/weilan/manager/trade_policy"})
@Api(value = "行业政策相关", description = "行业政策相关")
public class TradePolicyController extends BaseController {
    @Autowired
    TradePolicyService tradePolicyService;

    @ApiOperation(value = "删除一条行业政策信息", notes = "删除一条行业政策信息 ")
    @ApiImplicitParams({
    })
    @PostMapping(value = {"/del"})
    public ApiResponseBody del(
            @Valid @RequestBody DelBody body, BindingResult result,
            HttpServletRequest request) {
        tradePolicyService.del(body);
        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "编辑行业政策信息", notes = "编辑行业政策信息 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/modify"})
    public ApiResponseBody modify(
            @Valid @RequestBody TradePolicyModVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }
        tradePolicyService.modify(body);
        return ApiResponseBody.createSuccessBody(null);
    }


    @ApiOperation(value = "新增政策信息", notes = "新增政策信息 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/add"})
    public ApiResponseBody add(
            @Valid @RequestBody TradePolicyAddVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }
        tradePolicyService.add(body);
        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "删除批量", notes = "删除批量 ")
    @ApiImplicitParams({
    })
    @PostMapping(value = {"/del_list"})
    public ApiResponseBody delList(
            @Valid @RequestBody DelListBody body, BindingResult result,
            HttpServletRequest request) {

        tradePolicyService.delList(body);

        return ApiResponseBody.createSuccessBody(null);
    }
    @ApiOperation(value = "根据行业id获取政策法规列表", notes = "根据行业id获取政策法规列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "页数", required = false, paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页大小", required = false, paramType = "query", dataType = "int", defaultValue = "10")})
    @GetMapping(value = {"/get_list"})
    public ApiResponseBody<TradePolicyListVo> gettradeBlist(
            @RequestParam(value = "id", required = false) String id,
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "size", required = false, defaultValue = "10") int size,
            HttpServletRequest request) {

        Pageable pageable = new PageRequest(page - 1, size);

        TradePolicyListVo tradePolicyListVo = tradePolicyService.getListManager(id, pageable);

        return ApiResponseBody.createSuccessBody(tradePolicyListVo);
    }
}
